# pita: An invisible audio player
# Copyright (C) 2006-2007 Austin Bingham
#
# This program is free software# you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation# either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY# without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program# if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# You can reach the author at:
#     austin.bingham@gmail.com

from pita.decoder import *
import mad

class MAD(Decoder):
    def __init__(self, urlobj):
        Decoder.__init__(self)
        
        self.mfile = mad.MadFile(urlobj)
        
        if not self.mfile:
            raise CTORError('unable to open file %s' % urlobj.geturl())

        self.sample_rate = self.mfile.samplerate()

    def play_chunk_impl(self):
        buf = self.mfile.read()
        if buf is None:
            return Decoder.FINISHED
        else:
            self.device.play(buf, len(buf))
            return Decoder.OK
